/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SensorMain;

import DTO.ControlDTO;
import SensorInterface.ISensor;
import CommonValue.CommonString;
import CommonValue.CityName;
import CommonValue.NetConfig;
import CommonValue.SystemSign;
import CommonValue.SystemTimer;
import SocketBusiness.ClientConnect;
import DTO.SensorDTO;
import DTO.PacketDTO;
import SensorImpl.HumiditySensor;
import SensorImpl.PressureSensor;
import SensorImpl.TemperatureSensor;
import SensorImpl.WindSensor;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class ManagerSensor extends ClientConnect {

    private static final String systemName = SystemSign.SensorManager;
    List<ISensor> lstIS = new ArrayList<ISensor>();
    PacketDTO _packet = new PacketDTO(SystemSign.SensorManager);

    public static String getSystemName() {
        return systemName;
    }

    public ManagerSensor(String systemSign, String ServerIP, int port, ControlDTO operation) {
        super(systemSign, ServerIP, port, operation);
    }

    public final void installNewSensor(ISensor is) {
        lstIS.add(is);
    }

    public final void unInstallSensor(int i) {
        lstIS.remove(i);
    }

    @Override
    public void business() {
        boolean finish = false;
        SensorDTO o;
        int count = 0;
        do {
            try {
                Thread.sleep(SystemTimer.CollectDataSleep);
            } catch (InterruptedException ex) {
                Logger.getLogger(ManagerSensor.class.getName()).log(Level.SEVERE, null, ex);
            }

            if (!this.getOperation().getFlag()) {
                System.out.println(this.getSystemSign() + " DA DUNG HOAT DONG <<<<<<<<<<<<<<<<");
                continue;
            }
            
            for (ISensor t : lstIS) {
                count++;
                o = t.generateData();
                o.setSensorCount(count);
                System.out.println(o.getSensorSign() + " " + o.getCreatTime());
                System.out.println("count : >>>>>>>> " + o.getSensorCount());
                o.printData();
                _packet.addToBuffer(o);
            }

            try {
                this.sendMessage(_packet);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(ManagerSensor.class.getName()).log(Level.SEVERE, null, ex);
            }

            System.out.println("---------buffer size : " + _packet.getBuffer().size());

            _packet.clearBuffer();

        } while (!finish);
    }

    public static void main(String[] args) {

        System.out.println(systemName + CommonString.MainProgram);
        ControlDTO _controlMS = new ControlDTO(systemName, systemName); // tu no dieu khien chinh no

        ManagerSensor connectToWS = new ManagerSensor(systemName, NetConfig.WeatherSystemIP, NetConfig.WeaSys_Listen_SenMan_Port, _controlMS);
        connectToWS.installNewSensor(new TemperatureSensor(CityName.HANOI));
        connectToWS.installNewSensor(new WindSensor(CityName.HANOI));
        connectToWS.installNewSensor(new PressureSensor(CityName.HANOI));
        connectToWS.installNewSensor(new HumiditySensor(CityName.HANOI));
        Thread t1 = new Thread(connectToWS, systemName + CommonString.ThreadName + 1);
        t1.start();

    }
}
